package cn.ld.module.ai.controller.admin.model;

import cn.ld.framework.common.pojo.CommonResult;
import cn.ld.framework.common.pojo.PageResult;
import cn.ld.framework.common.util.object.BeanUtils;
import cn.ld.module.ai.controller.admin.model.vo.apikey.AiApiKeyPageInVO;
import cn.ld.module.ai.controller.admin.model.vo.apikey.AiApiKeyOutVO;
import cn.ld.module.ai.controller.admin.model.vo.apikey.AiApiKeySaveInVO;
import cn.ld.module.ai.controller.admin.model.vo.chatModel.AiChatModelOutVO;
import cn.ld.module.ai.dal.entity.model.AiApiKeyEntity;
import cn.ld.module.ai.dal.entity.model.AiChatModelEntity;
import cn.ld.module.ai.service.model.AiApiKeyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static cn.ld.framework.common.pojo.CommonResult.success;
import static cn.ld.framework.common.util.collection.CollectionUtils.convertList;

@Tag(name = "管理后台 - AI API 密钥")
@RestController
@RequestMapping("/ai/api-key")
@Validated
public class AiApiKeyController {

    @Resource
    private AiApiKeyService apiKeyService;

    @PostMapping("/create")
    @Operation(summary = "创建 API 密钥")
    @PreAuthorize("@ss.hasPermission('ai:api-key:create')")
    public CommonResult<Long> createApiKey(@Valid @RequestBody AiApiKeySaveInVO createInVO) {
        return success(apiKeyService.createApiKey(createInVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新 API 密钥")
    @PreAuthorize("@ss.hasPermission('ai:api-key:update')")
    public CommonResult<Boolean> updateApiKey(@Valid @RequestBody AiApiKeySaveInVO updateInVO) {
        apiKeyService.updateApiKey(updateInVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除 API 密钥")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ai:api-key:delete')")
    public CommonResult<Boolean> deleteApiKey(@RequestParam("id") Long id) {
        apiKeyService.deleteApiKey(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得 API 密钥")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ai:api-key:query')")
    public CommonResult<AiApiKeyOutVO> getApiKey(@RequestParam("id") Long id) {
        AiApiKeyEntity apiKey = apiKeyService.getApiKey(id);
        return success(BeanUtils.toBean(apiKey, AiApiKeyOutVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得 API 密钥分页")
    @PreAuthorize("@ss.hasPermission('ai:api-key:query')")
    public CommonResult<PageResult<AiApiKeyOutVO>> getApiKeyPage(@Valid AiApiKeyPageInVO pageInVO) {
        PageResult<AiApiKeyEntity> pageResult = apiKeyService.getApiKeyPage(pageInVO);
        return success(BeanUtils.toBean(pageResult, AiApiKeyOutVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得 API 密钥分页列表")
    public CommonResult<List<AiChatModelOutVO>> getApiKeySimpleList() {
        List<AiApiKeyEntity> list = apiKeyService.getApiKeyList();
        return success(convertList(list, key -> new AiChatModelOutVO().setId(key.getId()).setName(key.getName())));
    }

}